package com.microblog.microblogsystem.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.microblog.microblogsystem.context.BaseContext;
import com.microblog.microblogsystem.domain.dto.BlogPageQueryDTO;
import com.microblog.microblogsystem.domain.po.*;
import com.microblog.microblogsystem.domain.vo.BlogPageQueryVO;
import com.microblog.microblogsystem.domain.vo.BlogReadVO;
import com.microblog.microblogsystem.domain.vo.CommentVO;
import com.microblog.microblogsystem.mapper.BlogMapper;
import com.microblog.microblogsystem.result.PageResult;
import com.microblog.microblogsystem.result.Result;
import com.microblog.microblogsystem.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itlxc
 * @since 2024-06-28
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    private final BlogMapper blogMapper;


    /**
     * 分页查询博客信息
     * @param blogPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(BlogPageQueryDTO blogPageQueryDTO) {
        PageHelper.startPage(blogPageQueryDTO.getPage(),blogPageQueryDTO.getPageSize());
        // 获取当前用户id
        Long currentUserId = BaseContext.getCurrentId();
        blogPageQueryDTO.setUid(currentUserId);
        Page<BlogPageQueryVO> page = blogMapper.pageQuery(blogPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 用户点赞博客
     * @param bid
     */
    @Override
    public void like(Long bid) {
        Long uid = BaseContext.getCurrentId();
        // 判断该博客是否已经被点赞
        Likes likes = Db.lambdaQuery(Likes.class)
                .eq(Likes::getBid, bid)
                .eq(Likes::getUid, uid)
                .one();
        // 判断当前博客点赞状态，如果为1说明已被点赞，如果为2说明已被取消点赞，如果为null说明未点赞
        if (Objects.isNull(likes)) {
            log.info("当前博客状态为：未点赞");
            Likes like = Likes.builder()
                    .bid(bid)
                    .uid(uid)
                    .time(LocalDateTime.now())
                    .state(1)
                    .build();
            Db.save(like);
        } else {
            Integer state = likes.getState();
            log.info("当前博客点赞状态为：{}", state == 1 ? "已点赞" : "未点赞");
            // 取消点赞需要删除该点赞记录
            Db.removeById(likes.getId(), Likes.class);
        }
    }

    /**
     * 获取博客点赞数
     * @param bid
     * @return
     */
    private Long getLikeCount(Long bid) {
        return Db.lambdaQuery(Likes.class)
                .eq(Likes::getBid, bid)
                .count();
    }

    /**
     * 获取博客评论数
     * @param bid
     * @return
     */
    public Long getCommentCount(Long bid) {
        return Db.lambdaQuery(Comment.class)
                .eq(Comment::getBid, bid)
                .count();
    }

    /**
     * 阅读博客文章
     * @param bid
     * @return
     */
    @Override
    public BlogReadVO read(Long bid) {
        // 根据id查询博客文章内容
        BlogReadVO blogReadVO = blogMapper.getById(bid);
        blogReadVO.setCommentCount(getCommentCount(bid));
        blogReadVO.setLikeCount(getLikeCount(bid));
        // 获取博客文章评论信息
        List<CommentVO> commentVOList = blogMapper.getCommentById(bid);
        blogReadVO.setCommentVOList(commentVOList);
        return blogReadVO;
    }
}
